package business.cdss;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esotericsoftware.yamlbeans.YamlException;
import common.Data;
import common.RunCaseJson;
import common.Yaml;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import utils.GetFileMess;
import utils.YamlReadUtil;

import java.io.FileNotFoundException;
import java.util.ArrayList;

import static common.Login.login;

public class MatchWeight {

    Logger log = Logger.getLogger(MatchWeight.class);
    public static final String system = "env.properties";
    Data data = login();

    public JSONObject matchWeight(Object inpatientNo, String datas, Object hospitalCode) throws ConfigurationException, FileNotFoundException, YamlException {

        String path = new GetFileMess().getValue("ymlPath", system);
        Yaml yaml = YamlReadUtil.getLocatorMap(path, this.getClass().getName());
        JSONObject json = new JSONObject();
        json.put("access_token", data.getAccessToken());
        json.put("authToken", data.getAccessToken());
        json.put("uniqu_no", data.getUniquNo());
        json.put("operatorId", data.getUserId());
        json.put("authUserId", data.getUserId());
        json.put("authName", data.getUserName());
        json.put("org_ids", "true");
        json.put("inpatientNo", inpatientNo);
        json.put("data", datas);
        json.put("hospitalCode", hospitalCode);

        log.info("请求参数：" + json.toString());
        Response response = RunCaseJson.runCase(json, yaml.getMethod(), yaml.getUri());
        String jsonStr = response.asString();
        log.info("响应报文：" + jsonStr);
        JSONObject obj = JSON.parseObject(jsonStr);
        JSONObject jb = obj.getJSONObject("resultData");
//        JSONArray jsonArray=jb.getJSONArray("localData");
//        ArrayList list = new ArrayList();
//        for (Object object : jsonArray) {
//            JSONObject jsonObject = (JSONObject) object;
//            String inpatient_no = jsonObject.getString("inpatient_no");
//            String hospital_code = jsonObject.getString("hospital_code");
//            String a = inpatient_no + "|" + hospital_code;
//            list.add(a);
//        }
//        String inPatientNos = StringUtils.join(list, ",");
        return jb;
    }


    public static void main(String[] args) throws FileNotFoundException, ConfigurationException, YamlException {

        MatchWeight matchWeight = new MatchWeight();
        JSONObject a = matchWeight.matchWeight("ZY460000621110", "{\"年龄\":{\"num\":0,\"status\":0},\"性别\":{\"num\":0,\"status\":0},\"吸烟包年\":{\"num\":0,\"status\":0},\"吸烟史\":{\"num\":0,\"status\":0},\"是否有ICU转科\":{\"num\":0,\"status\":0},\"BMI\":{\"num\":0,\"status\":0},\"全部诊断\":{\"num\":0,\"status\":0},\"主要诊断\":{\"num\":10,\"status\":1},\"临床表现\":{\"num\":0,\"status\":0},\"主诉\":{\"num\":0,\"status\":0},\"现病史\":{\"num\":0,\"status\":0},\"家族史\":{\"num\":0,\"status\":0},\"既往史\":{\"num\":0,\"status\":0}}", "12440100455344205E");
        System.out.println(a.toString());
    }
}
